package com.heima;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Operation {
    OperationType type;
    List<DrawableShape> shapes;
    Map<DrawableShape, ShapeState> previousStates;
    Map<DrawableShape, ShapeState> newStates;

    Operation(OperationType type) {
        this.type = type;
        this.shapes = new ArrayList<>();
        this.previousStates = new HashMap<>();
        this.newStates = new HashMap<>();
    }

    void addShape(DrawableShape shape) {
        this.shapes.add(shape);
        saveCurrentState(shape);
    }

    void saveCurrentState(DrawableShape shape) {
        previousStates.put(shape, new ShapeState(shape));
    }

    void saveNewState(DrawableShape shape) {
        newStates.put(shape, new ShapeState(shape));
    }

    void restore() {
        for (Map.Entry<DrawableShape, ShapeState> entry : previousStates.entrySet()) {
            entry.getValue().applyTo(entry.getKey());
        }
    }

    void apply() {
        for (Map.Entry<DrawableShape, ShapeState> entry : newStates.entrySet()) {
            entry.getValue().applyTo(entry.getKey());
        }
    }
}